/*
 * Copyright (c) 2006, 2023, webrx.cn All rights reserved.
 *
 */

package cn.webrx;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;

/**
 * <p>Project: jse2303 - CheckDemo
 * <p>Powered by webrx On 2023-07-24 17:31:04
 * <p>描述：<p>
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class CheckDemo {
    public static void main(String[] args) throws Exception {
        check();
    }

    public static void check() throws Exception {
        int w = 160;
        int h = 60;
        int len = 5;
        Random rand = new Random();
        BufferedImage i = new BufferedImage(w, h, 2);
        Graphics2D g = i.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
        g.setColor(new Color(251, 251, 251));
        g.fillRect(0, 0, w, h);
        Font font = new Font("宋体", Font.BOLD, 15);
        font = font.deriveFont(Font.PLAIN, 55f);
        g.setFont(font);
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        for (int n = 0; n < 20; n++) {
            font = font.deriveFont(Font.PLAIN, rand.nextFloat() * 15 + 15);
            g.setFont(font);
            g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(10) + 50));
            int x = rand.nextInt(w);
            int y = rand.nextInt(h);
            g.drawString(String.valueOf(str.charAt(rand.nextInt(str.length()))), x, y);
        }
        StringBuilder sok = new StringBuilder(len);
        g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(25) + 220));
        for (int n = 0; n < len; n++) {
            font = font.deriveFont(Font.PLAIN, rand.nextFloat() * 35 + 30);
            g.setFont(font);
            int x = n * 32 + 5;
            int y = rand.nextInt(35) + 20;

            String sss = String.valueOf(str.charAt(rand.nextInt(str.length())));
            int degree = rand.nextInt(-38, 39);
            g.rotate(Math.toRadians(degree), (double) i.getWidth() / 2, (double) i.getHeight() / 2);
            sok.append(sss);
            g.drawString(sss, x, y);
            g.rotate(Math.toRadians(-degree), (double) i.getWidth() / 2, (double) i.getHeight() / 2);
        }
        Point[] points = {new Point(0, 0), new Point(10, 50), new Point(30, 6), new Point(60, 55), new Point(80, 3), new Point(160, 60)};
        GeneralPath path = new GeneralPath();
        path.moveTo(points[0].x, points[0].y);
        for (int i2 = 0; i2 < points.length - 1; ++i2) {
            Point sp = points[i2];
            Point ep = points[i2 + 1];
            Point c1 = new Point((sp.x + ep.x) / 2, sp.y);
            Point c2 = new Point((sp.x + ep.x) / 2, ep.y);
            path.curveTo(c1.x, c1.y, c2.x, c2.y, ep.x, ep.y);
        }
        g.setStroke(new BasicStroke(rand.nextInt(6) + 2));
        g.draw(path);
        g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(25) + 220));
        int yy = rand.nextInt(15) + 15;// y
        int hh = rand.nextInt(20) + 10;// 高度
        int aa = rand.nextInt(60) + 20;//
        for (int x = 10; x < w; x++) {
            int y = (int) (yy + hh * Math.sin(x * Math.PI / aa));
            g.fillOval(x, y, 3, 3);
        }
        // 输出验证码
        System.out.println(sok.toString());
        g.dispose();
        ImageIO.write(i, "png", new File("ok.png"));
        Runtime run = Runtime.getRuntime();
        run.exec("cmd /k start ok.png");
    }


}

